
//
//  Created by 黄思清 on 2017/3/9.
//  Copyright © 2017年 com.phpwsd. All rights reserved.
//

#import "PopWindowView.h"
#import "NSObject+Tips.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define RGBA(r, g, b, a) ([UIColor colorWithRed:(r / 255.0) green:(g / 255.0) blue:(b / 255.0) alpha:a])
#define RGB(r, g, b) RGBA(r,g,b,1)


typedef void(^doneBlock)(NSString *);

@interface PopWindowView ()<UIGestureRecognizerDelegate,UITextViewDelegate> {
    
}
@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;
@property (weak, nonatomic) IBOutlet UILabel *titLabel;
@property (weak, nonatomic) IBOutlet UIView *buttomView;
@property (weak, nonatomic) IBOutlet UIButton *doneBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;
- (IBAction)doneAction:(UIButton *)btn;
- (IBAction)cancelAction:(id)sender;
@property (weak, nonatomic) IBOutlet UITextView *contents;
@property (nonatomic,strong)doneBlock doneBlock;

@end

@implementation PopWindowView

-(instancetype)initWithCompleteBlock:(void(^)(NSString *))completeBlock {
    self = [super init];
    if (self) {
        self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] lastObject];
        
        [self setupUI];
        if (completeBlock) {
            self.doneBlock = ^(NSString *conText) {
                completeBlock(conText);
            };
        }
    }
    return self;
}

-(void)setupUI {
    
    self.buttomView.layer.cornerRadius = 2;
    self.buttomView.layer.masksToBounds = YES;
    self.themeColor = RGB(247, 133, 51);
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    
    //点击背景是否影藏
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeEnter)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    self.backgroundColor = RGBA(0, 0, 0, 0);
    [self layoutIfNeeded];
    
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    
    [self.titLabel setText:self.title];
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if( [touch.view isDescendantOfView:self.buttomView]) {
        return NO;
    }
    return YES;
}

-(void)setThemeColor:(UIColor *)themeColor {
    _themeColor = themeColor;
    self.doneBtn.titleLabel.textColor = themeColor;
    [self.titLabel setText:self.title];
    [self.titLabel setTextColor:themeColor];
    
    self.contents.layer.backgroundColor = [[UIColor clearColor] CGColor];
    self.contents.layer.borderColor = [RGBA(239,239,239,1) CGColor];
    self.contents.layer.borderWidth = 1.0;
    [self.contents.layer setMasksToBounds:YES];
    
    self.contents.text = self.placehoder;
    self.contents.textColor = [UIColor grayColor];
    self.contents.delegate = self;
    self.contents.font = [UIFont systemFontOfSize:16];
    
}

-(void)closeEnter{
     [self.contents resignFirstResponder];
}

#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if(textView.text.length < 1){
        textView.text = self.placehoder;
        textView.textColor = [UIColor grayColor];
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:self.placehoder]){
        textView.text=@"";
        textView.textColor=[UIColor blackColor];
    }
}
#pragma mark - Action
-(void)show {
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:.3 animations:^{
        self.bottomConstraint.constant = 10;
        self.backgroundColor = RGBA(0, 0, 0, 0.4);
        [self layoutIfNeeded];
    }];
}
-(void)dismiss {
    [UIView animateWithDuration:.3 animations:^{
       // self.bottomConstraint.constant = -self.height;
        self.backgroundColor = RGBA(0, 0, 0, 0);
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}


- (IBAction)doneAction:(UIButton *)btn {
    if([self.contents.text isEqualToString:self.placehoder]){
        [self showMessageTips:@"内容不能为空"];
    }else{
        self.doneBlock(self.contents.text);
        [self dismiss];
    }
}

#pragma mark - getter / setter
- (IBAction)cancelAction:(id)sender {
    [self dismiss];
}
@end
